<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    
  </style>
</head>
<body>
  
</body>
<script>

  // async function fn(){
  //   console.log(1)
  //   const res = await Promise.resolve("hello");
  //   console.log(res);
  //   console.log(2)
  // }
  
  // fn();


  // ==========
  
  // const p = Promise.resolve("hello");
  // p.then(res=>{
  //   console.log(res)
  // })
  // ↑↑↑等价于↓↓↓
  // const res = await Promise.resolve("hello");
  // console.log(res)

  // ==========


  const loginUrl = "http://10.11.55.249:3000/login";
  const getUserUrl = "http://10.11.55.249:3000/getuser";

  ;(async function(){
    let res = await ajax(loginUrl, {
      username:"admin",
      password:123456
    })

    res = JSON.parse(res);
    
    let res2 = await ajax(getUserUrl, {
      token: res.data.token
    })

    res2 = JSON.parse(res2);

    console.log(res);
    console.log(res2);
  })();

  

  function ajax(url, data){
    let str = "";
    for(let i in data){
      str += `${i}=${data[i]}&`
    }
    url += "?" + str.slice(0, -1);
    const xhr = new XMLHttpRequest()
    xhr.open("get", url, true);
    xhr.send();
    return new Promise((resolve, reject)=>{
      xhr.onload = function(){
        if(xhr.status === 200){
          resolve( xhr.responseText );
        }else{
          reject( url  + " " +  xhr.status );
        }
      }
    })
  }

  
</script>
</html>